// pages/receive/additionalInformation/additionalInformation.js
import api from '../../../utils/api';
import util from '../../../utils/util';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        basicData: '',
        inputData: {},
        show: false,
        minDate: '',
        maxDate: '',
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data.claimId = options.claimId;
        this.data.isCancle = options.isCancle;
        this.data.abolishId = options.abolishId;
    },
    //选择凭证日期
    onDisplay() {
        this.setData({
            minDate : new Date (2018, 0, 1).getTime(),
            maxDate: new Date(2028, 0, 31).getTime(),
            show: true
        });
    },
    onClose() {
        this.setData({
            show: false
        });
    },
    onConfirm(event) {
        let now = new Date(event.detail);
        this.data.inputData.voucherDate = util.formatTime(now).split(' ')[0];
        this.setData({
            show: false,
            inputData: this.data.inputData
        });
    },
    // 凭证编号
    onNumChange(e) {
        console.log(e)
        this.data.inputData.voucherCode = e.detail
    },
    //制单人
    onZdChange(e) {
        this.data.inputData.voucherOp = e.detail
    },
    // 基本信息
    getData() {
        let that = this;
        api.getClaimById({
            data: {
                claimId: that.data.claimId
            },
            success: function (res) {
                console.log(res)
                if (res.data.code == 200) {
                    that.data.basicData = res.data.data[0];
                    if (that.data.isCancle == '0') {
                        that.data.inputData = {
                            voucherCode: res.data.data[0].voucherCode,
                            voucherDate: res.data.data[0].voucherDate,
                            voucherOp: res.data.data[0].voucherOp
                        }
                    }
                    that.setData({
                        basicData: that.data.basicData,
                        inputData: that.data.inputData
                    })
                }
            }
        })
        if (that.data.isCancle == '1') {
            api.getClaimAbolishDataById({
                data: {
                    abolishId: that.data.abolishId
                },
                success: function (res) {
                    console.log(res)
                    if (res.data.code == 200) {
                        that.data.inputData = {
                            voucherCode: res.data.data[0].voucherCode,
                            voucherDate: res.data.data[0].voucherDate,
                            voucherOp: res.data.data[0].voucherOp
                        }
                        that.setData({
                            inputData: that.data.inputData
                        })
                    }
                }
            })
        }

    },
    // 保存
    onSave() {
        let that = this;
        if (that.data.isCancle) {
            api.saveAbolishClaim({
                data: {
                    id: that.data.abolishId,
                    voucherCode: that.data.inputData.voucherCode,
                    voucherDate: that.data.inputData.voucherDate,
                    voucherOp: that.data.inputData.voucherOp
                },
                method: 'POST',
                success: function (res) {
                    console.log(res)
                    if (res.data.code == 200) {
                        wx.showToast({
                            title: '保存成功',
                        })
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 1,
                            })
                        }, 100);
                    } else {
                        wx.showModal({
                            content: res.data.message,
                            showCancel: false,
                            confirmColor: '#0041C4'
                        })
                    }
                }
            })
        } else {
            api.saveClaim({
                data: {
                    id: that.data.claimId,
                    voucherCode: that.data.inputData.voucherCode,
                    voucherDate: that.data.inputData.voucherDate,
                    voucherOp: that.data.inputData.voucherOp
                },
                method: 'POST',
                success: function (res) {
                    console.log(res)
                    if (res.data.code == 200) {
                        wx.showToast({
                            title: '保存成功',
                        })
                        setTimeout(() => {
                            wx.navigateBack({
                                delta: 1,
                            })
                        }, 100);
                    } else {
                        wx.showModal({
                            content: res.data.message,
                            showCancel: false,
                            confirmColor: '#0041C4'
                        })
                    }
                }
            })
        }

    },
    // 取消
    onCancle() {
        wx.navigateBack({
            delta: 1,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getData()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    }
})